package com.iotechn.unimall.admin.api.rechargeableOrder;

import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.RechargeableOrderDO;
import com.iotechn.unimall.data.dto.rechargeable.RechargeableOrderUserDTO;
import com.iotechn.unimall.data.mapper.RechargeableOrderMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class RechargeableOrderServiceImpl implements RechargeableOrderService {
    @Autowired
    private RechargeableOrderMapper rechargeableOrderMapper;

    @Override
    public Page<RechargeableOrderUserDTO> page(Integer page, Integer limit,String phone) throws ServiceException {
        List<RechargeableOrderUserDTO> rechargeableOrderUserDTOS = rechargeableOrderMapper.selectRechargeableOrder(phone, (page - 1) * limit, limit);
        Integer count = rechargeableOrderMapper.selectRechargeableOrderCount(phone);
        return new Page<RechargeableOrderUserDTO>(rechargeableOrderUserDTOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RechargeableOrderDO create(RechargeableOrderDO rechargeableOrderDO, Long adminId) throws ServiceException {
//        rechargeableOrderDO.setAdminId(adminId);
        Date now=new Date();
        rechargeableOrderDO.setGmtCreate(now);
        rechargeableOrderDO.setGmtUpdate(now);
        if (rechargeableOrderMapper.insert(rechargeableOrderDO) > 0) {
            rechargeableOrderDO.setId(rechargeableOrderDO.getId());
            return rechargeableOrderDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(RechargeableOrderDO rechargeableOrderDO, Long adminId) throws ServiceException {
        Long id = rechargeableOrderDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now=new Date();
        rechargeableOrderDO.setGmtUpdate(now);
        if (rechargeableOrderMapper.updateById(rechargeableOrderDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (rechargeableOrderMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    public RechargeableOrderDO detail(Long id, Long adminId) throws ServiceException {
        return rechargeableOrderMapper.selectById(id);
    }

}